namespace Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText
{
    using System.Drawing;

    public class PhotoText : BaseText
    {
        protected int _grayLevel;
        protected char[] _replaceChars;	// 准备替换的字符数组

        public bool IsAntiColor { get; set; }

        public int GrayLevel
        {
            get { return _grayLevel; }
            set
            {
                _grayLevel = value;
                _replaceChars = (new ReplaceCharsList())[_grayLevel];
            }
        }

        public char[] ReplaceChars
        {
            get { return _replaceChars; }
            set
            {
                _replaceChars = value;
                _grayLevel = _replaceChars.Length;
            }
        }

        public PhotoText(Photo photo, int charsWidth, int charsHeight, char[] replaceChars)
            : base(photo, charsWidth, charsHeight)
        {
            ReplaceChars = replaceChars;
            IsAntiColor = false;
        }

        public PhotoText(Photo photo, int charsWidth, int charsHeight, int replaceCharIndex)
            : base(photo, charsWidth, charsHeight)
        {
            GrayLevel = replaceCharIndex;
            IsAntiColor = false;
        }

        public PhotoText(Photo photo, int charsWidth, int charsHeight)
            : this(photo, charsWidth, charsHeight, ReplaceCharsList.Chars8)
        {
        }

        public PhotoText(Photo photo)
            : this(photo, photo.Width, photo.Height)
        {
        }

        public PhotoText(System.Drawing.Bitmap image)
            : this(new Photo(image))
        {
        }

        public override string GenerateText()
        {
            return base.GenerateText();
        }

        protected override string ConvertPixelToString(Color color)
        {
            int sum = (color.R + color.G + color.B) / 3;
            if (IsAntiColor)
                sum = 255 - sum;
            int ci = sum * ReplaceChars.Length / 256;
            return ReplaceChars[ci].ToString();
        }
        /* Code no use.
         * 
                    // method 1 in GenerateText()
                    int wpc = _photo.Width / _charsWidth;		// width per char
                    int hpc = _photo.Height / _charsHeight;		// height per char

                    for (int h = 0; h < height; h++) {
                        for (int w = 0; w < width; w++) {
                            sb.Append(GetCharFromImageBlock(w, h, wp, hp, replaceChars));
                        }
                        sb.Append(System.Environment.NewLine);
                    }


                private char GetCharFromImageBlock(int width, int height, int wp, int hp, char[] replaceChars)
                {
                    int sum = 0;
                    int ws = width * wp;
                    int hs = height * hp;
                    try
                    {
                        for (int w = ws; w < ws + wp; w++)
                        {
                            for (int h = hs; h < hs + hp; h++)
                            {
                                // if (a >= _image.Height || b >= _image.Width) break;
                                System.Drawing.Color cc = _photo.GetPixel(w, h);

                                sum += (cc.R + cc.G + cc.B) / 3;
                            }
                        }
                    }
                    catch (Exception e) { }

                    sum /= wp * hp;

                    return replaceChars[(255 - sum) * replaceChars.Length / 256];
                }
        */
    }
}